package com.lzxuni.modules.Seed.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.common.utils.HumpUtils;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.poi.ExcelUtil;
import com.lzxuni.modules.Seed.entity.SeedInfo;
import com.lzxuni.modules.Seed.mapper.SeedInfoMapper;
import com.lzxuni.modules.Seed.service.SeedInfoService;
import com.lzxuni.modules.common.entity.PageParameter;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 天气信息_苗青管理 服务实现类
 * </p>
 *
 * @author LaoNa
 * @since 2020-12-07
 */
@Service
public class SeedInfoServiceImpl extends ServiceImpl<SeedInfoMapper, SeedInfo> implements SeedInfoService {

        @Override
        public PageInfo<SeedInfo> queryPage(PageParameter pageParameter, SeedInfo seedInfo) {
            if (StringUtils.isNotEmpty(pageParameter.getSidx())) {
                PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                        HumpUtils.humpToLine(pageParameter.getSidx()) + " " + pageParameter.getSord());
            } else {
                PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows());
            }
            List<SeedInfo> seedInfoList = baseMapper.queryList(seedInfo);
            PageInfo<SeedInfo> pageInfo = new PageInfo<>(seedInfoList);
            return pageInfo;
        }

        @Override
        public List<SeedInfo> queryList(SeedInfo seedInfo) {
            List<SeedInfo> seedInfoList = baseMapper.queryList(seedInfo);
            return seedInfoList;
        }
    @Override
    public Workbook exportExcel(SeedInfo seedInfo) throws IOException {
        PageHelper.orderBy("create_date" + " " + "DESC");
        LinkedHashMap<String, List<List<String>>> dataMap = new LinkedHashMap<>();
        List<List<String>> listData1 = new ArrayList<>();
        Workbook wb = null;
        List<SeedInfo> seedInfoList =baseMapper.queryList(seedInfo);
        List<String> list0 = new ArrayList<>();
        list0.add("所属地块");
        list0.add("作物名称");
        list0.add("作物类型");
        list0.add("日期");
        list0.add("苗情长势");

        list0.add("苗情状态");
        list0.add("指导方案");


        listData1.add(list0);
        if (seedInfoList != null && seedInfoList.size() > 0) {
            for (int i = 0; i < seedInfoList.size(); i++) {
                SeedInfo seedInfo1 = seedInfoList.get(i);
                List<String> list = new ArrayList<>();

                list.add(seedInfo1.getLandNum());
                list.add(seedInfo1.getCropName());
                list.add(seedInfo1.getCropModel());
                list.add(DateFormatUtils.format(seedInfo1.getCreateDate(),"yyyy-MM-dd"));
                list.add(seedInfo1.getSeedName1());
                list.add(seedInfo1.getSeedState1());
                list.add(seedInfo1.getLimitScheme());

                listData1.add(list);
            }
        }
        dataMap.put("苗情管理", listData1);
        ExcelUtil excelUtil = new ExcelUtil();
        wb = excelUtil.writeExcel(dataMap, "苗情管理");
        return wb;
    }
}
